<template>
    <div class="user">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>活动管理</el-breadcrumb-item>
            <el-breadcrumb-item>活动列表</el-breadcrumb-item>
            <el-breadcrumb-item>活动详情</el-breadcrumb-item>
        </el-breadcrumb>
        <div style="margin-top: 15px; w">
            <el-input placeholder="请输入内容" class="input-with-select" style="width:300px" v-model="searchValue" @keydown.native.enter="searchData">
                <el-button slot="append" icon="el-icon-search" @click="searchData"></el-button>

            </el-input>
            <el-button type="success" plain @click="shoWaddUser">添加用户</el-button>

            <template>
                <el-table :data="tableData3" height="250" border style="width: 100%">
                    <el-table-column type="index" width="50">
                    </el-table-column>
                    <el-table-column prop="username" label="姓名" width="180">
                    </el-table-column>
                    <el-table-column prop="email" label="邮箱" width="180">
                    </el-table-column>
                    <el-table-column prop="mobile" label="电话">
                    </el-table-column>
                    <el-table-column label="状态">
                        <template slot-scope="scope">
                            <el-switch active-color="#13ce66" inactive-color="#ff4949" v-model="scope.row.mg_state" @change='changeStatus(scope.row)'>
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-tooltip content="编辑" placement="top">
                                <el-button plain type="primary" icon="el-icon-edit" @click="ShowhandleEdit(scope.row.id)"></el-button>
                            </el-tooltip>
                            <el-tooltip content="授权角色" placement="top">
                                <el-button plain type="primary" icon="el-icon-share" @click="Showgrant(scope.row)"></el-button>
                            </el-tooltip>
                            <el-tooltip content="删除" placement="top">
                                <el-button plain type="primary" icon="el-icon-delete" @click="delUser(scope.row.id)"></el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>

                </el-table>
            </template>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>

        <!-- 添加用户 -->
        <el-dialog title="添加用户" :visible.sync="addialogFormVisible">
            <el-form :model="addData" :rules="rules" ref="addData">
                <el-form-item label="用户名" prop="username" :label-width="formLabelWidth">
                    <el-input v-model="addData.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
                    <el-input v-model="addData.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
                    <el-input v-model="addData.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
                    <el-input v-model="addData.mobile" auto-complete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addUserSend('addData')">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 编辑用户 -->
        <el-dialog title="添加用户" :visible.sync="editdialogFormVisible">
            <el-form :model="editData" :rules="rules" ref="editData">
                <el-form-item label="用户名" prop="username" :label-width="formLabelWidth">
                    <el-input v-model="editData.username" auto-complete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
                    <el-input v-model="editData.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
                    <el-input v-model="editData.mobile" auto-complete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editUserSend('editData')">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 授权角色 -->
        <el-dialog title="授权角色" :visible.sync="grantdialogFormVisible">
            <el-form :model="grantform">
                <el-form-item label="活动名称" :label-width="formLabelWidth">
                    <el-input v-model="grantform.username" auto-complete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="活动区域" :label-width="formLabelWidth">
                    <!-- select中的v-model可以获取到option里面的id值 -->
                    <el-select v-model="grantform.rid" placeholder="请选择活动区域">
                        <el-option v-for="item in grantData" :key="item.id" :value="item.id" :label="item.roleName"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="grantDataSend(grantform)">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
  getUserdata,
  add,
  searchId,
  edit,
  del,
  editStatus,
  grantData,
  renewGrant
} from "@/api/index.js";
export default {
  data() {
    return {
      data: "",
      tableData3: [],
      pagenum: 1,
      pagesize: 10,
      total: 0,
      searchValue: "",
      addData: {
        username: "",
        mobile: "",
        email: "",
        password: ""
      },
      editData: {
        username: "",
        email: "",
        mobile: "",
        id: ""
      },
      grantform: {
        id: "",
        rid: "",
        username: ""
      },
      grantData: [],
      addialogFormVisible: false,
      editdialogFormVisible: false,
      grantdialogFormVisible: false,
      formLabelWidth: "88px",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        mobile: [{ required: true, message: "请输入电话", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    //   当前页变化
    handleCurrentChange(val) {
      console.log(val);
      this.pagenum = val;
      this.initList();
    },
    //   每一页的数据的数量
    handleSizeChange(val) {
      console.log(val);
      this.pagesize = val;
      this.initList();
    },
    initList() {
      getUserdata({
        query: this.searchValue,
        pagenum: this.pagenum,
        pagesize: this.pagesize
      }).then(res => {
        console.log(res);
        this.tableData3 = res.data.users;
        this.total = res.data.total;
      });
    },
    // 弹出添加用户的框框
    shoWaddUser() {
      this.addialogFormVisible = true;
    },
    // 添加用户
    addUserSend(newUser) {
      // this.addialogFormVisible = false
      console.log(newUser);
      this.$refs[newUser].validate(valid => {
        if (valid) {
          add(this.addData).then(val => {
            console.log(val);
            if (val.meta.status == 201) {
              this.$message({
                showClose: true,
                message: val.meta.msg,
                type: "success"
              });

              // 添加成功后初始化数据
              this.initList();
              //    文本对话框消失
              this.addialogFormVisible = false;
            }
          });
        } else {
        }
      });
    },

    // 搜索数据
    searchData() {
      this.initList();
    },
    // 展示编辑框
    ShowhandleEdit(id) {
      this.editdialogFormVisible = true;
      this.editData.id = id;
      // 同时查询响应的用户数据
      searchId(id).then(val => {
        this.editData.username = val.data.username;
        this.editData.mobile = val.data.mobile;
        this.editData.email = val.data.email;
      });
    },
    // 编辑用户
    editUserSend(formdata) {
      this.$refs[formdata].validate(valid => {
        if (valid) {
          edit(this.editData).then(val => {
            if (val.meta.status == 200) {
              // 初始化数据
              this.initList();

              this.$message({
                showClose: true,
                message: val.meta.msg,
                type: "success"
              });
              this.editdialogFormVisible = false;
            }
          });
        }
      });
    },
    // 删除用户
    delUser(id) {
      this.$confirm(`此操作将永久删除id为${id}的用户数据, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del(id).then(val => {
            console.log(val);
            if (val.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.initList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 修改用户状态
    changeStatus(row) {
      editStatus(row).then(val => {
        if (val.meta.status == 200) {
          this.$message({
            showClose: true,
            message: val.meta.msg,
            type: "success"
          });
        }
      });
    },
    // 展示授权角色框
    Showgrant(data) {
      this.grantform.username = data.username;
      this.grantform.id = data.id;
      this.grantdialogFormVisible = true;
      grantData().then(val => {
        console.log(val);
        this.grantData = val.data;
      });
    },
    // 授权角色
    grantDataSend(grantform) {
      console.log(grantform);
      renewGrant(grantform).then(val => {
        console.log(val);
        if (val.meta.status == 200) {
          this.$message({
            showClose: true,
            message: val.meta.msg,
            type: "success"
          });
          this.grantdialogFormVisible =false
        }
      });
    }
  },
  mounted() {
    //   初始化数据
    this.initList();
  }
};
</script>

<style scoped lang='scss'>
.el-breadcrumb {
  background-color: #f5f5f5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
.user {
  .search-input {
    width: 300px;
  }
}
</style>